package com.system.function.service;

import com.system.Tools.TimeTools;
import com.system.function.dao.DeliveryDAO;
import com.system.function.object.Commodity;
import com.system.function.object.Delivery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeliveryService {
    @Autowired
    private DeliveryDAO DeliveryDAO;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TimeTools timeTools;

    //查询全部
    public List<Delivery> getAllDelivery(Delivery Delivery){
        List<Delivery> allDelivery = new ArrayList<>();
        try{
            List<Delivery> allDeliverySearch = DeliveryDAO.getAllDelivery(Delivery);
            for (Delivery DeliverySearch: allDeliverySearch) {
                Commodity commodity = new Commodity();
                commodity.setId(DeliverySearch.getCommodityId());
                Commodity commoditySearch = commodityService.getCommodity(commodity);
                //更新入库信息数据
                if(null != commoditySearch){
                    if(!commoditySearch.getName().equals(DeliverySearch.getCommodityName())){
                        DeliverySearch.setCommodity(commoditySearch);
                        //更新数据
                        updateDelivery(DeliverySearch);
                    }
                    DeliverySearch.setCommodity(commoditySearch);

                    allDelivery.add(DeliverySearch);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return allDelivery;
    }
    //查询总数
    public Integer getCountDelivery(Delivery Delivery){
        return DeliveryDAO.getCountDelivery(Delivery);
    }

    //查询
    public Delivery getDelivery(Delivery Delivery){
        Delivery DeliverySearch = null;
        try{
            DeliverySearch = DeliveryDAO.getDelivery(Delivery);
            Commodity commodity = new Commodity();
            commodity.setId(DeliverySearch.getCommodityId());
            Commodity commoditySearch = commodityService.getCommodity(commodity);
            //更新入库信息数据
            if(!commoditySearch.getName().equals(DeliverySearch.getCommodityName())){
                DeliverySearch.setCommodity(commoditySearch);
                //更新数据
                updateDelivery(DeliverySearch);
            }
            DeliverySearch.setCommodity(commoditySearch);

        }catch (Exception e){
            e.printStackTrace();
        }
        return DeliverySearch;
    }

    //增加
    public String addDelivery(Delivery Delivery){
        String isSuccess = "false";
        try{
            //入库时 更新指定商品库存
            Commodity commodity = new Commodity();
            commodity.setId(Delivery.getCommodityId());
            //Commodity commoditySearch = commodityService.getCommodity(commodity);
            //更新库存
            /*Integer stock = Integer.parseInt(commoditySearch.getStock());
            Integer num = Delivery.getNum();
            if((stock - num) < 0){
                return  "LessThanInventory";
            }
            commoditySearch.setStock(String.valueOf(stock - num));*/

            /*commodityService.updateCommodity(commoditySearch);*/
            Delivery.setId(timeTools.getTimeInt());
            int resultNum = DeliveryDAO.addDelivery(Delivery);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
    //删除
    public String delDelivery(Delivery Delivery){
        String isSuccess = "false";
        try{
            int resultNum  = DeliveryDAO.delDelivery(Delivery);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return isSuccess;
    }
    //修改
    public String updateDelivery(Delivery Delivery){
        String isSuccess = "false";
        try{
            int resultNum = DeliveryDAO.updateDelivery(Delivery);
            if(resultNum>0){
                isSuccess = "true";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }

    public String updateExamine(Delivery Delivery){
        String isSuccess = "false";
        try{
            if("通过审核".equals(Delivery.getExamineState())){
                //入库时 更新指定商品库存
                Commodity commodity = new Commodity();
                commodity.setId(Delivery.getCommodityId());
                Commodity commoditySearch = commodityService.getCommodity(commodity);
                //更新库存
                Integer stock = Integer.parseInt(commoditySearch.getStock());
                Integer num = Delivery.getNum();
                if((stock - num) < 0){
                    return  "LessThanInventory";
                }

                commoditySearch.setStock(String.valueOf(stock - num));
                commodityService.updateCommodity(commoditySearch);
                int resultNum = DeliveryDAO.updateDelivery(Delivery);
                if(resultNum>0){
                    isSuccess = "true";
                }
            }else{
                int resultNum = DeliveryDAO.updateDelivery(Delivery);
                if(resultNum>0){
                    isSuccess = "true";
                }
            }


        }catch (Exception e){
            e.printStackTrace();
            return "false-has-double";
        }
        return isSuccess;
    }
}
